import { Menu ,app,BrowserWindow,Tray,ipcMain,nativeImage,session } from 'electron' // eslint-disable-line
const path = require('path');

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\') // eslint-disable-line
}

const startOnBoot = require('./startOnBoot.js');
const exeName = 'pzycxwfx';

let mainWindow = null;
// 托盘对象
let appTray = null;

const winURL = process.env.NODE_ENV === 'development'
  ? 'http://localhost:9080'
  : `file://${__dirname}/index.html`;

/* eslint-disable */
const shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
  // 当另一个实例运行的时候，这里将会被调用，我们需要激活应用的窗口
  if (mainWindow) {
    if (mainWindow.isMinimized()) mainWindow.show();
    if (!mainWindow.isVisible()) mainWindow.show();
    mainWindow.focus();
  }
  return true;
});

if (shouldQuit){
  app.quit();
  /* eslint-disable */
  ab();
}
function ab() {
  return false;
}

function createWindow() {
  /**
   * Initial window options
   */
  // Menu.setApplicationMenu(null);

  mainWindow = new BrowserWindow({
    height: 800,
    useContentSize: true,
    width: 1500,
    show: false,
    nodeIntegrationInWorker: true,
  });

  const trayIcon = path.join(__dirname, '../../../../icon.ico');
  const nImage = nativeImage.createFromPath(trayIcon);
  appTray = new Tray(nImage);

  // 图标的上下文菜单
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '显示主页面',
      click() {
        mainWindow.show();
      },
    },
    {
      label: '帮助',
      click() {},
    },
    {
      label: '退出',
      click() {
        if (process.platform !== 'darwin') {
          appTray.destroy();
          mainWindow.destroy();
          app.quit();
        }
      },
    },
  ]);
  // 设置此托盘图标的悬停提示内容
  appTray.setToolTip('派驻检察室执法异常行为分析系统');

  // 设置此图标的上下文菜
  appTray.setContextMenu(contextMenu);

  appTray.on('click', () => {
    if (mainWindow.isVisible()) {
      mainWindow.hide();
    } else {
      mainWindow.show();
    }
  });
  mainWindow.once('ready-to-show', () => {
    mainWindow.show();
  });

  mainWindow.loadURL(winURL);

  mainWindow.on('closed', () => {

  });
  mainWindow.on('close', (event) => {
    mainWindow.hide();
    event.preventDefault();
  });
}


app.on('ready', createWindow);


app.on('window-all-closed', () => {
  // mainWindow.hide();
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on('window-show', () => {
  mainWindow.show();
});
app.on('activate', () => {
  if (mainWindow === null) {
    createWindow();
  }
});

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */

ipcMain.on('getAutoStartValue', () => {
  startOnBoot.getAutoStartValue(exeName, (error, result) => {
      if (error) {
        mainWindow.webContents.send('getAutoStartValue', false);
      } else {
        mainWindow.webContents.send('getAutoStartValue', true);
      }
  });
});

ipcMain.on('enableAutoStart', () => {
  console.log('设置开机自动启动');
  startOnBoot.enableAutoStart(exeName, process.execPath);
});

ipcMain.on('trayhide', () => {
  if(!appTray.isDestroyed()){
    appTray.destroy();
  }
});



ipcMain.on('trayshow', () => {
  if(appTray.isDestroyed()){
    const trayIcon = path.join(__dirname, '../../../../icon.ico');
    const nImage = nativeImage.createFromPath(trayIcon);
    appTray = new Tray(nImage);
  
    // 图标的上下文菜单
    const contextMenu = Menu.buildFromTemplate([
      {
        label: '显示主页面',
        click() {
          mainWindow.show();
        },
      },
      {
        label: '帮助',
        click() {},
      },
      {
        label: '退出',
        click() {
          if (process.platform !== 'darwin') {
            appTray.destroy();
            mainWindow.destroy();
            app.quit();
          }
        },
      },
    ]);
    // 设置此托盘图标的悬停提示内容
    appTray.setToolTip('派驻检察室执法异常行为分析系统');
  
    // 设置此图标的上下文菜
    appTray.setContextMenu(contextMenu);
  
    appTray.on('click', () => {
      console.log('1');
      if (mainWindow.isVisible()) {
        mainWindow.hide();
      } else {
        mainWindow.show();
      }
    });
  }
});

ipcMain.on('checkalram', () => {
  
  mainWindow.webContents.send('checknewalarm');
});

ipcMain.on('note', () => {
  if (mainWindow.isVisible()) {
  } else {
    mainWindow.show();
  }
});


